package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONArray;
import com.sailfish.springbootdemo.dao.db1.*;
import com.sailfish.springbootdemo.dao.db4.UserDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.*;
import com.sailfish.springbootdemo.utils.CommonUtils;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class PcbaDeviceAgeHandledServiceImpl implements PcbaDeviceAgeHandledService {

    @Autowired
    private MaintenanceConfigDao maintenanceConfigDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private BrandTypeDao brandTypeDao;

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private PcbaDeviceAgeHandledDao pcbaDeviceAgeHandledDao;

    @Autowired
    private PcbaCompanyDao pcbaCompanyDao;

    @Autowired
    private PcbaWorkshopDao pcbaWorkshopDao;

    @Autowired
    private DeviceAgeHistoryDao deviceAgeHistoryDao;

    @Autowired
    private PcbaLineDao pcbaLineDao;

    @Autowired
    private PcbaDeviceTypeDao pcbaDeviceTypeDao;

    @Autowired
    private PcbaDeviceResourceDao pcbaDeviceResourceDao;

    @Autowired
    private PcbaDeviceDao pcbaDeviceDao;

    @Autowired
    private LineLevelDao lineLevelDao;

    @Autowired
    private CoreAlgorithmService coreAlgorithmService;

    @Override
    public Result queryAllAssetNumberDevices(String chooseCompanyAndWorkshop, String assetNumber, Integer handledType, Integer pcbaDeviceTypeId, Integer brandId, Integer brandTypeId, Integer handledStatus, Integer page, Integer pageSize) {
        try{

            int begin = (page - 1) * pageSize;

            List<Integer> pcbaWorkshopIdList = new ArrayList<>();
            if (chooseCompanyAndWorkshop != null && !"".equals(chooseCompanyAndWorkshop)) {
                JSONArray jsonArray = JSONArray.parseArray(chooseCompanyAndWorkshop);
                for (int a = 0; a < jsonArray.size(); a++) {
                    JSONArray jsonArray1 = jsonArray.getJSONArray(a);
                    if (jsonArray1.size() == 2) {
                        pcbaWorkshopIdList.add(jsonArray1.getInteger(1));
                    }
                }
            }

            Double dayOfHighLine = 6 * 365d;
            Double dayOfDevice = 6 * 365d;
            List<MaintenanceConfig> maintenanceConfigByType1 = maintenanceConfigDao.getMaintenanceConfigByType(1);
            if(maintenanceConfigByType1.size() > 0){
                dayOfHighLine = maintenanceConfigByType1.get(0).getMaintenanceConfigRange2() * 365d;
            }
            List<MaintenanceConfig> maintenanceConfigByType2 = maintenanceConfigDao.getMaintenanceConfigByType(2);
            if(maintenanceConfigByType2.size() > 0){
                dayOfDevice = maintenanceConfigByType2.get(0).getForewarningAge() * 365d;
            }
            List<PcbaDeviceAgeHandled> list = pcbaDeviceAgeHandledDao.queryAllAssetNumberDevices(assetNumber, handledType, dayOfHighLine, dayOfDevice, pcbaDeviceTypeId, pcbaWorkshopIdList, handledStatus, begin, pageSize);
            List<PcbaDeviceAgeHandled> list2 = pcbaDeviceAgeHandledDao.queryAllAssetNumberDevices(assetNumber, handledType, dayOfHighLine, dayOfDevice, pcbaDeviceTypeId, pcbaWorkshopIdList, handledStatus, null, null);
            for (PcbaDeviceAgeHandled pcbaDeviceDeviceAgeHandled : list) {
                if (pcbaDeviceDeviceAgeHandled.getProductTime() != null && !"".equals(pcbaDeviceDeviceAgeHandled.getProductTime().toString())) {
                    Date d1 = new Date();
                    LocalDate ld1 = d1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                    LocalDate ld2 = pcbaDeviceDeviceAgeHandled.getProductTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                    double age = CommonUtils.calculateYearDifference(ld2, ld1);
                    pcbaDeviceDeviceAgeHandled.setAge(age);
                }
                Integer pcbaLineId = pcbaDeviceDeviceAgeHandled.getPcbaLineId();
                if(pcbaLineId != null){
                    PcbaLine pcbaLine = pcbaLineDao.getPcbaLineById(pcbaLineId);
                    if(pcbaLine != null){
                        pcbaLine.setPcbaCompany(pcbaCompanyDao.getPcbaCompanyById(pcbaLine.getPcbaCompanyId()));
                        pcbaLine.setPcbaWorkshop(pcbaWorkshopDao.getPcbaWorkshopById(pcbaLine.getPcbaWorkshopId()));
                        LineLevel lineLevel = lineLevelDao.getLineLevelById(pcbaLine.getPcbaLineLevel());
                        pcbaLine.setLineLevel(lineLevel);
                        pcbaDeviceDeviceAgeHandled.setPcbaLine(pcbaLine);
                        pcbaDeviceDeviceAgeHandled.setPcbaLine(pcbaLine);
                    }
                }
                Integer pcbaDeviceId = pcbaDeviceDeviceAgeHandled.getPcbaDeviceId();
                if(pcbaDeviceId != null){

                    // 查询是否存在pcbaDevice中
                    PcbaDevice pd = pcbaDeviceDao.getById(pcbaDeviceId);
                    if(pd != null){
                        pcbaDeviceDeviceAgeHandled.setIsInPcbaDevice(1);
                    }else{
                        pcbaDeviceDeviceAgeHandled.setIsInPcbaDevice(2);
                    }

                    List<DeviceAgeHistory> deviceAgeHistoryByDeviceId1 = deviceAgeHistoryDao.getDeviceAgeHistoryByDeviceId(pcbaDeviceId, 1);
                    for (DeviceAgeHistory deviceAgeHistory : deviceAgeHistoryByDeviceId1) {
                        Integer submitterId = deviceAgeHistory.getSubmitterId();
                        if(submitterId != null){
                            deviceAgeHistory.setSubmitter(userDao.getUserInfoById(submitterId));
                        }
                        Integer auditorId = deviceAgeHistory.getAuditorId();
                        if(auditorId != null){
                            deviceAgeHistory.setAuditor(userDao.getUserInfoById(auditorId));
                        }
                        List<String> closeFilesList = new ArrayList<>();
                        if(deviceAgeHistory.getCloseFiles() != null && !"".equals(deviceAgeHistory.getCloseFiles())){
                            String[] split = deviceAgeHistory.getCloseFiles().split(",");
                            closeFilesList.addAll(Arrays.asList(split));
                        }
                        deviceAgeHistory.setCloseFilesList(closeFilesList);
                    }
                    pcbaDeviceDeviceAgeHandled.setDeviceAgeHistoryListOfHighLine(deviceAgeHistoryByDeviceId1);
                    List<DeviceAgeHistory> deviceAgeHistoryByDeviceId2 = deviceAgeHistoryDao.getDeviceAgeHistoryByDeviceId(pcbaDeviceId, 2);
                    for (DeviceAgeHistory deviceAgeHistory : deviceAgeHistoryByDeviceId2) {
                        Integer submitterId = deviceAgeHistory.getSubmitterId();
                        if(submitterId != null){
                            deviceAgeHistory.setSubmitter(userDao.getUserInfoById(submitterId));
                        }
                        Integer auditorId = deviceAgeHistory.getAuditorId();
                        if(auditorId != null){
                            deviceAgeHistory.setAuditor(userDao.getUserInfoById(auditorId));
                        }
                        List<String> closeFilesList = new ArrayList<>();
                        if(deviceAgeHistory.getCloseFiles() != null && !"".equals(deviceAgeHistory.getCloseFiles())){
                            String[] split = deviceAgeHistory.getCloseFiles().split(",");
                            closeFilesList.addAll(Arrays.asList(split));
                        }
                        deviceAgeHistory.setCloseFilesList(closeFilesList);
                    }
                    pcbaDeviceDeviceAgeHandled.setDeviceAgeHistoryListOfCommonLine(deviceAgeHistoryByDeviceId2);

                    PcbaDevice pcbaDevice = pcbaDeviceDao.getById(pcbaDeviceDeviceAgeHandled.getPcbaDeviceId());

                    if(pcbaDevice != null && pcbaDeviceDeviceAgeHandled.getPcbaLine() != null && pcbaDeviceDeviceAgeHandled.getPcbaLine().getPcbaLineLevel() != null){
                        Result result = coreAlgorithmService.calcIsOverSixYearInHighLine(pcbaDevice, pcbaDeviceDeviceAgeHandled.getPcbaLine().getPcbaLineLevel());
                        int i = Integer.parseInt(result.getData().toString());
                        pcbaDeviceDeviceAgeHandled.setIsOverSixYearInHighLine(i);
                    }else{
                        pcbaDeviceDeviceAgeHandled.setIsOverSixYearInHighLine(null);
                    }
                }
                Integer pcbaDeviceTypeId_ = pcbaDeviceDeviceAgeHandled.getPcbaDeviceTypeId();
                if(pcbaDeviceTypeId_ != null){
                    PcbaDeviceType byId = pcbaDeviceTypeDao.getById(pcbaDeviceTypeId_);
                    if(byId != null){
                        pcbaDeviceDeviceAgeHandled.setPcbaDeviceType(byId);
                    }
                }
                Integer pcbaDeviceResourceId = pcbaDeviceDeviceAgeHandled.getPcbaDeviceResourceId();
                if(pcbaDeviceResourceId != null){
                    PcbaDeviceResource byId = pcbaDeviceResourceDao.getById(pcbaDeviceResourceId);
                    if(byId != null){
                        Integer brandId_ = byId.getBrandId();
                        if(brandId_ != null){
                            Brand brandById = brandDao.getBrandById(brandId_);
                            if(brandById != null){
                                byId.setBrand(brandById);
                            }
                        }
                        Integer brandTypeId_ = byId.getBrandTypeId();
                        if(brandTypeId_ != null){
                            BrandType brandTypeById = brandTypeDao.getBrandTypeById(brandTypeId_);
                            if(brandTypeById != null){
                                byId.setBrandType(brandTypeById);
                            }
                        }
                        pcbaDeviceDeviceAgeHandled.setPcbaDeviceResource(byId);
                    }
                }
            }
            return ResultUtil.tableSuccess(page, pageSize, list2.size(), list);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
